package eliminate.controller;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.swing.JFrame;

import eliminate.model.chess.ChessModel;
import eliminate.view.ChessView;

@SuppressWarnings("serial")
public class NetGame extends JFrame {
	public static ChessModel chessModel = new ChessModel();
	ChessView chessView = new ChessView(chessModel);
    Socket socket;
    
    private int[][] chessViewData = new int[chessModel.getHorizontalNum()][chessModel.getVerticalNum()];
    PrintWriter writer;
    
	public NetGame(Socket socket) {
		this.socket = socket;
		new ReadData(socket).start();
		
		chessView.addMouseListener(new ChessViewListener());
		chessModel.addObserver(chessView);

		this.setContentPane(chessView);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	class ChessViewListener extends MouseAdapter {

		int start_value;
		int start_x;
		int start_y;

		int end_x;
		int end_y;
		int end_value;

		boolean isNotInLine = false;

		public ChessViewListener() {
		}

		public void mousePressed(MouseEvent e) {
			start_x = e.getX() / chessModel.getWidth();
			start_y = e.getY() / chessModel.getHeight();
		}

		public void mouseReleased(MouseEvent e) {
	
			end_x = e.getX() / chessModel.getWidth() - start_x;
			end_y = e.getY() / chessModel.getHeight() - start_y;
			if (Math.abs(end_x) >= Math.abs(end_y)) {
				if (end_x < 0 && start_x > 0) {
					end_x = start_x - 1;
				} else if (end_x > 0
						&& start_x < chessModel.getHorizontalNum() - 1) {
					end_x = start_x + 1;
				} else {
					return;
				}
				end_y = start_y;
			} else {
				if (end_y < 0 && start_y > 0) {
					end_y = start_y - 1;
				} else if (end_y > 0
						&& start_y < chessModel.getVerticalNum() - 1) {
					end_y = start_y + 1;
				} else {
					return;
				}
				end_x = start_x;
			}
			try {
				writer = new PrintWriter(socket.getOutputStream());
				System.out.println("Print");
				writer.println(start_x + " " + start_y + " " + end_x + " " + end_y);
				writer.flush();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	class ReadData extends Thread {
		private Socket socket;
		public ReadData(Socket socket) {
			this.socket = socket;
		}

		public void run() {

			BufferedReader reader = null;

			try {

				reader = new BufferedReader(new InputStreamReader(

				socket.getInputStream()));

				String message = null;

				while (true) {

					message = reader.readLine();
					if (message.equals("bye")) {
						break;
					}
					String[] s = message.split("\\s+");
					if(s.length == 4) {
						chessModel.swapColor(Integer.parseInt(s[0]), Integer.parseInt(s[1]), 
								Integer.parseInt(s[2]), Integer.parseInt(s[3]));
					}
					else{
						for(int i = 0; i < chessModel.getHorizontalNum(); i++) {
							for(int j = 0; j < chessModel.getVerticalNum(); j++) {
								chessViewData[i][j] = Integer.parseInt(s[i * chessModel.getHorizontalNum() + j]);
							}
						}
						chessModel.setData(chessViewData);
					}

					System.out.println(message);

				}

			} catch (IOException e) {

				e.printStackTrace();

			} finally {
				try {
					if (reader != null) {
						reader.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}

			}

		}

	}
	
	public static void main(String[] args) {
		try {
			Socket socket = new Socket("127.0.0.1", 8001);
			JFrame frame = new NetGame(socket);
			frame.setSize(800, 600);
			frame.setVisible(true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
